# Replication file for
# Maggie Shum
# When Voting Turnout Becomes Contentious Repertoire: How Anti-ELAB Protest Overtook the District Council Election in Hong Kong 2019

rm(list = ls())
library(plyr)
library(dplyr)
library(tidyr)
library(RColorBrewer)
library(ggplot2)
library(Rmisc)

dce_15 = read.csv("District_Council_2015.csv", header = TRUE)
dce_19 = read.csv("District_council_2019_pop.csv", header = TRUE)

# broken down by ideology + type of org
cdce_15 = summarySE(dce_15, measurevar="winning_mar_perc", groupvars=c("win_party_ideo"), na.rm = TRUE)
# Standard error of the mean
ggplot(cdce_15, aes(x=win_party_ideo, y=winning_mar_perc)) + 
  geom_errorbar(aes(ymin=winning_mar_perc-se, ymax=winning_mar_perc+se), 
                width=.1, position=position_dodge(0.1)) +
  geom_point(position=position_dodge(0.1)) + 
  geom_text(aes(label=round(winning_mar_perc, digits = 1), hjust=-0.5, vjust=0.5))+
  xlab("Political Camps") +
  ylab("Winning Margin in %") +
  scale_colour_hue(name="Type of organization",    # Legend label, use darker colors
                   breaks=c("org", "party"),
                   labels=c("Political group", "Political party"),
                   l=40) +                    # Use darker colors, lightness=40
  ggtitle("The effect of political camp \n on winning margin (2015)") +
  theme_bw() +
  theme(legend.justification=c(1,0),
        legend.position=c(1,0))    


# broken down by ideology + type of org
cdce_19 = summarySE(dce_19, measurevar="winning_mar_perc", groupvars=c("win_party_ideo"), na.rm = TRUE)
# Standard error of the mean
ggplot(cdce_19, aes(x=win_party_ideo, y=winning_mar_perc)) + 
  geom_errorbar(aes(ymin=winning_mar_perc-se, ymax=winning_mar_perc+se), 
                width=.1, position=position_dodge(0.1)) +
  geom_point(position=position_dodge(0.1)) + 
  geom_text(aes(label=round(winning_mar_perc, digits = 1), hjust=-0.5, vjust=0.5))+
  xlab("Political Camps") +
  ylab("Winning Margin in %") +
  scale_colour_hue(name="Type of organization",    # Legend label, use darker colors
                   breaks=c("org", "party"),
                   labels=c("Political group", "Political party"),
                   l=40) +                    # Use darker colors, lightness=40
  ggtitle("The effect of political camp \n on winning margin (2019)") +
  theme_bw() +
  theme(legend.justification=c(1,0),
        legend.position=c(1,0))   





